#ifndef __SCRIPT_EXPRESSION_H__
#define __SCRIPT_EXPRESSION_H__

#include <stdlib.h>
#include "script_var.h"
#include "script_func.h"
#include "script_string.h"

struct exp_node_t;
struct exec_func_node;
struct script_t;

typedef enum session_expression_type{
	OPERTOR_TYPE_EVALUATION,	// =
	OPERTOR_TYPE_REFER,			// =>	指向操作
	OPERTOR_TYPE_PLUSEVALUATION, // +=
	OPERTOR_TYPE_SUBTEVALUATION, // -=
	OPERTOR_TYPE_EQUAL,			// ==
	OPERTOR_TYPE_UNEQUAL,		// !=
	OPERTOR_TYPE_GREATER,		// >
	OPERTOR_TYPE_GREATER_EQUAL,	// >=
	OPERTOR_TYPE_LESS,			// <
	OPERTOR_TYPE_LESS_EQUAL,	// <=
	OPERTOR_TYPE_PLUS,			// +
	OPERTOR_TYPE_SUBTRACTION,	// -
	OPERTOR_TYPE_MULTIPLY,		// *
	OPERTOR_TYPE_DIVISION,		// /
	OPERTOR_TYPE_REMAINDER,		// %
	OPERTOR_TYPE_PARENTHESIS_L,	// (
	OPERTOR_TYPE_PARENTHESIS_R,	// )
	OPERTOR_TYPE_DOT,			// .
	OPERTOR_TYPE_COMMA,			// ,
	OPERTOR_TYPE_BRACKET_L,		// [
	OPERTOR_TYPE_BRACKET_R,		// ]
	OPERTOR_TYPE_AND,			// &&
	OPERTOR_TYPE_OR,			// ||
	OPERTOR_TYPE_NOR,			// !
	OPERTOR_TYPE_BITAND,		// &
	OPERTOR_TYPE_BITXOR,		// ^
	OPERTOR_TYPE_BITOR,			// |
	OPERTOR_TYPE_BITNOR,		// ~
	OPERTOR_TYPE_LEFTSHIFT,		// <<
	OPERTOR_TYPE_RIGHTSHIFT,	// >>
	OPERTOR_TYPE_MAX	
}operator_type_e;

typedef struct operator_t{
	unsigned char type;			// operator_type_e
	unsigned char priority;
	char *oper;
	struct exp_node_t *(*calc)(struct exp_node_t *left,struct exp_node_t *right);
}operator_t;

typedef enum expression_node_type{
	EXP_NODE_TYPE_CONST_NULL,
	EXP_NODE_TYPE_CONST_INT,
	EXP_NODE_TYPE_CONST_DOUBLE,
	EXP_NODE_TYPE_CONST_STRING,
	EXP_NODE_TYPE_CONST_TRUE,
	EXP_NODE_TYPE_CONST_FALSE,
	EXP_NODE_TYPE_VAR,
	EXP_NODE_TYPE_VAR_NAME,
	EXP_NODE_TYPE_OPERTOR,
	EXP_NODE_TYPE_FUNC,
	EXP_NODE_TYPE_ERR,
}exp_node_type_e;

typedef enum expression_node_ctrl{
	EXP_NODE_CTRL_NONE,
	EXP_NODE_CTRL_RETURN,		// 退出函数代码块
	EXP_NODE_CTRL_BREAK,		// 退出循环代码块
	EXP_NODE_CTRL_CONTINUE,		// 循环continue
	EXP_NODE_CTRL_BREAKPOINT,	// 断点退出
}exp_node_ctrl_e;

/********************************************************
 *	Data exp_node_t
 *		表达式节点
 *		用于表达式运算
 * ******************************************************/
typedef struct exp_node_t{
	//exp_node_type_e		type;
	//exp_node_ctrl_e		ctrl;		// 用于控制代码块行为
	unsigned char type;
	unsigned char ctrl;
	unsigned char priority;
	union{
		int					number;
		double					d;
		char					*string;
		char					*var_name;
		exp_var_t				*var;
		const operator_t			*oper;
		struct exec_func_node	*func;
	};
	struct exp_node_t *next;
}exp_node_t;

typedef struct exp_parser_t{
	exp_string_t *strs;		// 字符串列表
	exp_string_t *c_str;	// 当前处理的字符串

	script_stack_t *stack;

	exp_node_t *exp_list;	// 表达式链表
	exp_node_t *cnode;		// 当前表达式节点
	exp_node_t *prev_node;	// 前一个表达式节点

	struct script_t  *script;	// 脚本指针

	unsigned char flag:2; // 0: parsing; 1: parse done; 2: parse fail
	unsigned char dot_flag:1; // 1: prev operate is "."
	unsigned char operator_flag:2;	// 当前解析状态 
	unsigned char prev_operator_flag:2;	// 
	unsigned char exec_func_arg_flag:1;	// 1: parse exec function argument

	int pare_count;
	int bracket_count;

}exp_parser_t;

int session_expression_analysis(char *str);

void exp_node_print(exp_node_t *node);

void exp_list_print(exp_node_t *head);

exp_node_t *exp_node_create(exp_node_type_e type, void *value);

void exp_node_destroy(exp_node_t *node);

void exp_node_list_destroy(exp_node_t *node);

void show_exp_string_t(struct exp_string_t *exp);

exp_node_t *expression_decode(exp_string_t *exp_str, struct script_t *script);

exp_node_t *expression_calc(exp_node_t *exp, script_stack_t *block_stack);

int expression_str(exp_parser_t *parser);

exp_node_t *expression_parse(char *code, struct script_t *script);

void get_exp_node_value(exp_node_t *node, exp_node_type_e *type, char **str, int *num,double *d);

char *exp_node_to_string(exp_node_t *node);

extern exp_node_t *evaluation(exp_node_t *left, exp_node_t *right);

const operator_t *opertor_get(char *str);

int judge(exp_node_t *value);
#endif

